/**
 * 
 */
package t4j.innerclasses;

/**
 * @author sz
 *
 * create time：2012-9-19
 */

interface Selector {

	boolean end();
	Object current();
	void next();
}


public class Sequence {
	
	private Object[] items;
	
	private int next = 0;
	
	public Sequence(int size ){ items = new Object[size];}
	
	public void add(Object x){
		if(next < items.length)
			items[next++] = x;
	}
	
	private class SequenceSelector implements Selector{
		private int i = 0;
	
		public boolean end() {
			return i == items.length;
		}
		
		public Object current() {
			return items[i];
		}

		public void next() {
			if( i < items.length) 
				i++;
		}			
	}
	
	private class reverseSelector implements Selector{
		
		private int i = items.length -1 ;
		
		public boolean end() {
			return i == -1;
		}
		
		public Object current() {
			return items[i];
		}

		public void next() {
			if( i >= 0) 
				i--;
		}	
	}
	
	public Selector selecor(){
		return new SequenceSelector();
	}
	
	public Selector getReverseSelector(){
		return new reverseSelector();
	}
	
//	public static void main(String[] args) {
//		
//		Sequence sequence = new Sequence(10);
//		
//		for(int i=0 ; i < 10 ; i++)
//			sequence.add(i);
//		
//		Selector selector = sequence.selecor();
//		
//		while(!selector.end()){
//			System.out.print(selector.current());
//			selector.next();
//		}
//		
//	}
	
	public static void main(String[] args) {
		Sequence sequence = new Sequence(10);
		
		for(int i=0 ; i < 10 ; i++)
		   sequence.add(i);
		
		Selector selector = sequence.getReverseSelector();
		
		while(!selector.end()){
			System.out.println(selector.current());
			selector.next();
		}
	}
}
